package me.zhengjie.pm.projectstaff.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.pm.projectstaff.domain.ProjectStaff;
import me.zhengjie.pm.projectstaff.service.ProjectStaffService;
import me.zhengjie.pm.projectstaff.service.dto.ProjectStaffQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Api(tags = "项目人员管理")
@RestController
@RequestMapping("/api/projectStaff")
public class ProjectStaffController {

    private final ProjectStaffService projectStaffService;

    public ProjectStaffController(ProjectStaffService projectStaffService) {
        this.projectStaffService = projectStaffService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('projectStaff:list')")
    public void download(HttpServletResponse response, ProjectStaffQueryCriteria criteria) throws IOException {
        projectStaffService.download(projectStaffService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询项目人员")
    @ApiOperation("查询项目人员")
    @PreAuthorize("@el.check('projectStaff:list')")
    public ResponseEntity<Object> getProjectStaffs(ProjectStaffQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(projectStaffService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增项目人员")
    @ApiOperation("新增项目人员")
    @PreAuthorize("@el.check('projectStaff:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ProjectStaff resources){
        return new ResponseEntity<>(projectStaffService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改项目人员")
    @ApiOperation("修改项目人员")
    @PreAuthorize("@el.check('projectStaff:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ProjectStaff resources){
        projectStaffService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除项目人员")
    @ApiOperation("删除项目人员")
    @PreAuthorize("@el.check('projectStaff:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        projectStaffService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}